package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.XfCompany;
import org.dromara.system.domain.query.XfCompanyQuery;
import org.dromara.system.domain.vo.XfCompanyVo;
import org.dromara.system.mapper.XfCompanyMapper;
import org.dromara.system.service.IXfCompanyService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class XfCompanyServiceImpl extends ServiceImpl<XfCompanyMapper, XfCompany> implements IXfCompanyService {

    @Override
    public TableDataInfo<XfCompanyVo> selectPageXfCompanyList(XfCompanyQuery query) {
        // 创建分页对象
        Page<XfCompany> page = new Page<>(query.getPageNum(), query.getPageSize());

        // 创建查询条件包装器
        LambdaQueryWrapper<XfCompany> wrapper = Wrappers.lambdaQuery();

        // 根据企业ID查询
        wrapper.eq(query.getCompanyId() != null, XfCompany::getCompanyId, query.getCompanyId());

        // 根据企业名称模糊查询
        wrapper.like(StringUtils.isNotBlank(query.getCompanyName()), XfCompany::getCompanyName, query.getCompanyName());

        // 根据行业查询
        wrapper.eq(StringUtils.isNotBlank(query.getCompanyIndustry()), XfCompany::getCompanyIndustry, query.getCompanyIndustry());

        // 根据地区查询
        if (query.getLocation() != null) {
            if (StringUtils.isNotBlank(query.getLocation().getCountry())) {
                wrapper.apply("JSON_UNQUOTE(JSON_EXTRACT(company_location, '$.country')) = {0}", query.getLocation().getCountry());
            }
            if (StringUtils.isNotBlank(query.getLocation().getProvince())) {
                wrapper.apply("JSON_UNQUOTE(JSON_EXTRACT(company_location, '$.province')) = {0}", query.getLocation().getProvince());
            }
            if (StringUtils.isNotBlank(query.getLocation().getCity())) {
                wrapper.apply("JSON_UNQUOTE(JSON_EXTRACT(company_location, '$.city')) = {0}", query.getLocation().getCity());
            }
        }
        // 根据企业ID集合查询
//        wrapper.in(query.getCompanyIds() != null && !query.getCompanyIds().isEmpty(), XfCompany::getCompanyId, query.getCompanyIds());

        // 按创建时间降序排列
        wrapper.orderByDesc(XfCompany::getCreateTime);

        // 执行分页查询
        Page<XfCompanyVo> result = baseMapper.selectPageXfCompanyList(page, wrapper);
        return TableDataInfo.build(result);

    }

    @Override
    public List<XfCompanyVo> selectXfCompanyList(XfCompanyQuery query) {
        return baseMapper.queryList(query);
    }

    @Override
    public XfCompanyVo selectXfCompanyById(Long companyId) {
        return baseMapper.selectXfCompanyById(companyId);
    }

    @Override
    public boolean insertXfCompany(XfCompany company) {
        return this.save(company);
    }

    @Override
    public boolean updateXfCompany(XfCompany company) {
        return this.updateById(company);
    }

    @Override
    public int deleteXfCompanyById(Long companyId) {
        return baseMapper.deleteById(companyId);
    }

    @Override
    public int deleteXfCompanyByIds(Long[] companyIds) {
        return baseMapper.deleteBatchIds(List.of(companyIds));
    }
}
